package com.tour.tour.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tour.tour.common.Response;
import com.tour.tour.dto.TourRouteDTO;
import com.tour.tour.entity.TourRouteEntity;
import com.tour.tour.exception.AuthorizationForbiddenException;
import com.tour.tour.exception.UserStatusException;
import com.tour.tour.mapper.TourRouteMapper;
import com.tour.tour.service.TourRouteService;
import com.tour.tour.utils.UserInfoUtil;
import com.tour.tour.vo.TourRouteVO;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.tour.tour.utils.constant.RedisConstant.LOGIN_USER_KEY;

/**
 * @author 76004
 * @apiNote
 * @date 2023/4/27 19:24
 */
@Service
public class TourRouteServiceImpl extends ServiceImpl<TourRouteMapper, TourRouteEntity>implements TourRouteService {

    public Response<Void> update(TourRouteEntity tourRouteEntity, HttpServletRequest httpServletRequest) throws UserStatusException, AuthorizationForbiddenException {
        String uid = UserInfoUtil.getUid(httpServletRequest);

        if(uid.equals(tourRouteEntity.getUid())){
            updateById(tourRouteEntity);
        }else throw new AuthorizationForbiddenException("你没有权限访问");
        return Response.success();
    }


    public Response<List<TourRouteVO>> getPage(Integer pageNum,Integer pageSize) throws UserStatusException {
        List<TourRouteEntity> tourRouteEntities = page(new Page<TourRouteEntity>(pageNum, pageSize),
                new QueryWrapper<TourRouteEntity>().eq("status",1)).getRecords();

        List<TourRouteVO>tourRouteVOS = tourRouteEntities.stream().map(tourRouteEntity -> {
            TourRouteVO tourRouteVO = new TourRouteVO();
            BeanUtil.copyProperties(tourRouteEntity,tourRouteVO);
            return tourRouteVO;
        }).collect(Collectors.toList());

        return Response.success(tourRouteVOS);
    }

    public Response<Void>delete(Long tid,HttpServletRequest httpServletRequest) throws UserStatusException, AuthorizationForbiddenException {
        String uid = UserInfoUtil.getUid(httpServletRequest);
        TourRouteEntity tourRouteEntity = getById(tid);
        if(!uid.equals(tourRouteEntity.getUid())){
            throw new AuthorizationForbiddenException("你没有权限删除");
        }
        removeById(tid);
        return Response.success();
    }

    @Override
    public Response<Void> addTourRoute(TourRouteDTO tourRouteDTO,HttpServletRequest httpServletRequest) throws UserStatusException, AuthorizationForbiddenException {
        String uid = UserInfoUtil.getUid(httpServletRequest);
        tourRouteDTO.setUid(uid);
        TourRouteEntity tourRouteEntity = new TourRouteEntity();
        BeanUtil.copyProperties(tourRouteDTO,tourRouteEntity);
        tourRouteEntity.setCreateTime(new Date());
        save(tourRouteEntity);
        return Response.success();
    }
}
